<?php
class Delivery_Model_DbTable_Clientes
	extends Delivery_Model_DbTable_Desactivable

{
	protected $_name = 'clientes';
	protected $_primary = 'idCliente';
	
	public function insert( $cliente ){
		
		return parent::insert( 
			$this->_fecha( $this->_getData( $cliente ), true, true )	
		);	
	}
	
	public function update( $cliente ){
		
		return parent::update(
			$this->_fecha( $this->_getData( $cliente ), false, true ),
			array('idCliente = ?' => $cliente->idCliente)
		
		);
	}
	
	public function delete( $cliente ){
		return parent::delete(
			array( 'idCliente = ?' => $cliente->idCliente)
		);
	}
	
	
	public function getById( $idCliente ){
		
		return 
			$this->_db->fetchRow(
				$this->select()->where('idCliente = ?', $idCliente)
			);
	}
	
	/**
	 * @param array $ids
	 * @return array:
	 */
	public function getByIds( $ids )
	{
		return $this->getAdapter()->fetchAll(
			$this->select()->where( 'idCliente IN (?)', $ids, Zend_Db::INT_TYPE),
			null,
			Zend_Db::FETCH_ASSOC
		);		
	}
	
	public function getByFacebookId( $fbUserId ){
		
		return 
			$this->_db->fetchRow(
				$this->select()->where('fbUserId = ?', $fbUserId)
			);
	}
	
	
	public function getByEmail( $email ){
		return 
			$this->_db->fetchRow(
				$this->select()->where('email = ?', $email)
			);
	}
	
	public function getByAccount( $email, $clave ){
		
		return 
			$this->_db->fetchRow(
				$this->select()
					->where('email = ?', $email)
					->where('clave = ?', $clave)
					->where('activo = ?', 1)					
			);
	}
	
	protected function _getData( $cliente )
	{
		return array (
			'email'		=> $cliente->email,
   			'clave'		=> $cliente->clave,
   			'nombre'	=> $cliente->nombre,
   			'apellido'	=> $cliente->apellido,
   			'telefono'	=> $cliente->telefono,
   			'activo'	=> $cliente->activo,
   			'codigo'	=> $cliente->codigo,
			'cantidadPedidos'=> $cliente->cantidadPedidos,
   			'fbUserId'	=> $cliente->fbUserId
   		);
	
	} 
	
}